<template>
  <div>
    <router-link v-show="showAbs" tag="div" to="/" class="header-abs">
      <div class="iconfont icon-back">&#xe971;</div>
    </router-link>
    <div :style="opacityStyle" v-show="!showAbs" class="header-fixed">
      景点详情
      <router-link
        class="iconfont icon-back"
        tag="div"
        to="/"
      >
        &#xe971;
      </router-link>
    </div>
  </div>
</template>

<script>
export default {
  name: 'DetailHeader',
  data () {
    return {
      showAbs: true,
      opacityStyle: {
        opacity: 0
      }
    }
  },
  methods: {
    handleScroll () {
      let top = document.documentElement.scrollTop
      if (top > 0) {
        let opacity = top / 140
        opacity = opacity > 1 ? 1 : opacity
        this.opacityStyle = { opacity }
        this.showAbs = false
      } else {
        this.showAbs = true
      }
    }
  },
  activated () {
    window.addEventListener('scroll', this.handleScroll)
  },
  // 对全局事件进行解绑，因为scroll绑定在window对象中，
  // 所有页面都将存在，当前组件隐藏时进行移除
  deactivated () {
    window.removeEventListener('scroll', this.handleScroll)
  }
}
</script>

<style lang="stylus" scoped>
  @import '~styles/variable.styl'
  .header-abs
    position absolute
    left 0
    top 0
    margin .05rem
    width .76rem
    height .76rem
    line-height .76rem
    text-align center
    border-radius .76rem
    color #fff
    background-color rgba(0,0,0,.6)
    .icon-back
      font-size .4rem
  .header-fixed
    position fixed
    top 0
    left 0
    height $headerHeight
    line-height $headerHeight
    width 100%
    text-align center
    z-index 2
    opacity .4
    color #fff
    background-color #00bcd4
    .icon-back
      position absolute
      left 0
      top 0
      font-size .4rem
      width .86rem
      text-align center
  .content
    height 50rem
</style>
